#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-07-01 16:58
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：
import sqlite_client
import access_client
import mysql_client
import mssql_client
import oracle_client
import logging

log = logging.getLogger('main.database_client')


# 获取连接字典
def get_connect_dict(config):
    config_dict = {row.get('name'): row for row in config}
    connect_dict = {}
    for key, config in config_dict.items():
        _type = config.get('type', '').lower()

        if _type == 'access':
            connect_dict[key] = access_client.Connect(config)
            continue

        if _type == 'mysql':
            connect_dict[key] = mysql_client.Connect(config)
            continue

        if _type in ('sql server', 'mssql', 'sqlserver'):
            connect_dict[key] = mssql_client.Connect(config)
            continue

        if _type == 'oracle':
            connect_dict[key] = oracle_client.Connect(config)
            continue

        if _type == 'sqlite':
            connect_dict[key] = sqlite_client.Connect(config)
        if _type =='s7':
            pass
        else:
            log.warning("连接：{}，参数：{}，不是常用数据库，无法连接".format(key, config))
    return connect_dict


# 获取连接列表
def get_connect_list(config):
    connect_dict = get_connect_dict(config)
    if connect_dict:
        return list(connect_dict.values())
    else:
        return []